#/usr/bin/env bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"

set -e

function change_files_content() {
    local from=$1
    local to=$2
    # .hpp文件修改
    find include -name "*.hpp" |xargs sed -i "s/${from}/${to}/g"
    find python -name "*.hpp" |xargs sed -i "s/${from}/${to}/g"
    # .h文件修改
    find src -name "*.h" |xargs sed -i "s/${from}/${to}/g"
    find python -name "*.h" |xargs sed -i "s/${from}/${to}/g"
    find include -name "*.h" |xargs sed -i "s/${from}/${to}/g"
    # .cpp文件修改
    find src -name "*.cpp" |xargs sed -i "s/${from}/${to}/g"
    find matlab -name "*.cpp" |xargs sed -i "s/${from}/${to}/g"
    find python -name "*.cpp" |xargs sed -i "s/${from}/${to}/g"
    find tools -name "*.cpp" |xargs sed -i "s/${from}/${to}/g"
    find examples -name "*.cpp" |xargs sed -i "s/${from}/${to}/g"
    # .c文件修改
    find python -name "*.c" |xargs sed -i "s/${from}/${to}/g"
    # Makefile修改
    sed -i "s/${from}/${to}/g" Makefile
    # CMakeLists
    find . -name "CMakeLists.txt" |xargs sed -i "s/${from}/${to}/g"
    find . -name "*.cmake" |xargs sed -i "s/${from}/${to}/g"
    find . -name "Makefile.config.*" |xargs sed -i "s/${from}/${to}/g"
    # .py文件修改
    find src -name "*.py" |xargs sed -i "s/${from}/${to}/g"
    find scripts -name "*.py" |xargs sed -i "s/${from}/${to}/g"
    find python -name "*.py" |xargs sed -i "s/${from}/${to}/g"
    find tools -name "*.py" |xargs sed -i "s/${from}/${to}/g"
    find examples -name "*.py" |xargs sed -i "s/${from}/${to}/g"
    # .cu文件
    find src -name "*.cu" |xargs sed -i "s/${from}/${to}/g"
    find include -name "*.cuh" |xargs sed -i "s/${from}/${to}/g"
    find python -name "*.cu" |xargs sed -i "s/${from}/${to}/g"
    # 其他一些奇怪的文件
    find src -name "*.cpp.common" |xargs sed -i "s/${from}/${to}/g"
    find src -name "*.cpp.fc" |xargs sed -i "s/${from}/${to}/g"
    find src -name "*.cpp.bak" |xargs sed -i "s/${from}/${to}/g"
}

function change_files_name() {
    local from=$1
    local to=$2
    local a=($(find . -name ${from} -type f))
    # echo "${a[@]}"
    for oFile in "${a[@]}"
    do
        d=$( dirname ${oFile} )
        nFile="${d}/${to}"
        mv -v ${oFile} ${nFile}
    done
}

function change_dirs_name() {
    local from=$1
    local to=$2
    local a=($(find . -name ${from} -type d))
    for oDir in "${a[@]}"
    do
        d=$( dirname ${oDir} )
        nDir="${d}/${to}"
        mv -v ${oDir} ${nDir}
    done
}

function change_module () {
    local from=$1
    local to=$2
    # 修改文件名称
    change_files_name ${from}.hpp ${to}.hpp
    change_files_name ${from}.cpp ${to}.cpp
    # include文件
    change_files_content ${from} ${to}
}

function change_file_content() {
    local fName=$1
    local from=$2
    local to=$3

    ps=($( find . -name ${fName} -type f))
    for p in "${ps[@]}" 
    do
        file_exists_or_die "${p}"
        # echo sed -i "s/${from}/${to}/g" "${p}"
        sed -i "s/${from}/${to}/g" "${p}"
    done
}

# 修改文件内容
echo "change file content tsm_caffe --> ts_caffe ..."
change_files_content tsm_caffe_float ts_caffe_float
change_files_content tsm_caffe ts_caffe
change_files_content tsm_open_file ts_open_file
change_files_content TsmIOException TsIOException
change_files_content TSM_ TS_

# 修改目录名称
echo "change dir names tsm_caffe --> ts_caffe ..."
change_dirs_name tsm_caffe ts_caffe
change_dirs_name tsm_caffe_float ts_caffe_float
if [ -e matlab/+tsm_caffe ]
then
    mv -v matlab/+tsm_caffe matlab/+ts_caffe
fi
change_dirs_name tsm_tests ts_tests

# tsm_caffe.proto
if [ -e ./src/ts_caffe/proto/tsm_caffe.proto ]
then
    echo "tsm_caffe.proto --> ts_caffe.proto"
    mv ./src/ts_caffe/proto/tsm_caffe.proto ./src/ts_caffe/proto/ts_caffe.proto
fi
echo "Change ts_caffe.proto content"
change_file_content ts_caffe.proto tsm_caffe ts_caffe
change_file_content ts_caffe.proto TSMSliceParameter TSSliceParameter
change_file_content ts_caffe.proto tsm_slice_param ts_slice_param

# 修改一些文件名和#include语句
echo "change tsm_utils module..."
change_module tsm_utils ts_utils
# 宏定义
change_files_content _TSM_UTILS_ _TS_UTILS_

echo "change tsm_io module..."
change_module tsm_io ts_io
# 宏定义
change_files_content _TSM_IO_ _TS_IO_

# 修改一些层文件名和层名称
echo "Change layer TSMSlice --> TSSlice"
change_file_content tsm_slice_layer.hpp CAFFE_TSM_SLICE_LAYER_HPP_ CAFFE_TS_SLICE_LAYER_HPP_
change_file_content tsm_slice_layer.hpp TSMSliceLayer TSSliceLayer
change_file_content tsm_slice_layer.hpp TSMSlice TSSlice
change_file_content tsm_slice_layer.cpp CAFFE_TSM_SLICE_LAYER_HPP_ CAFFE_TS_SLICE_LAYER_HPP_
change_file_content tsm_slice_layer.cpp TSMSliceLayer TSSliceLayer
change_file_content tsm_slice_layer.cpp TSMSlice TSSlice
change_file_content tsm_slice_layer.cpp tsm_slice_param ts_slice_param
# include
change_files_content tsm_slice_layer ts_slice_layer
# 文件名称
change_module tsm_slice_layer ts_slice_layer

echo "Change layer TSMSliceFix --> TSSliceFix"
change_file_content tsm_slice_fix_layer.hpp CAFFE_TSM_SLICE_FIX_LAYER_HPP_ CAFFE_TS_SLICE_FIX_LAYER_HPP_
change_file_content tsm_slice_fix_layer.hpp TSMSliceFixLayer TSSliceFixLayer
change_file_content tsm_slice_fix_layer.hpp TSMSliceFix TSSliceFix
change_file_content tsm_slice_fix_layer.cpp CAFFE_TSM_SLICE_FIX_LAYER_HPP_ CAFFE_TS_SLICE_FIX_LAYER_HPP_
change_file_content tsm_slice_fix_layer.cpp TSMSliceFixLayer TSSliceFixLayer
change_file_content tsm_slice_fix_layer.cpp TSMSliceFix TSSliceFix
change_file_content tsm_slice_fix_layer.cpp tsm_slice_param ts_slice_param
change_file_content tsm_slice_fix_layer.cpp TSMSliceParameter TSSliceParameter
# include
change_files_content tsm_slice_fix_layer ts_slice_fix_layer
# 文件名称
change_module tsm_slice_fix_layer ts_slice_fix_layer

echo "Etc change..."
change_file_content roi_align_fix_layer.cpp tsm_debug ts_debug
change_file_content .gitignore tsm_ ts_
change_file_content Makefile TSM TS
change_files_name _tsm_caffe.cpp _ts_caffe.cpp
change_files_name _tsm_caffe_float.cpp _ts_caffe_float.cpp
